package db;
import java.io.*;
import java.net.URL;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.JasperDesign;







public class SalesReport extends HttpServlet
{

  private static String retrievejrxmlpath = null;
  private static String generatereportpath = null;
  
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
  {
    ServletOutputStream servletOutputStream = response.getOutputStream();

   Connection con = DBConnector.connect();
    byte[] bytes = null;
    Calendar currentDate = Calendar.getInstance();
    SimpleDateFormat formatter=  new SimpleDateFormat("MMMMddyyyy");
    SimpleDateFormat timeformat = new SimpleDateFormat("hhmma");
    String timeNow = timeformat.format(currentDate.getTime());
    String dateNow = formatter.format(currentDate.getTime());
    
    Properties p = new Properties();
                try 
                {
                    p.load(SalesReport.class.getResourceAsStream("reportdirectory.properties"));
                    retrievejrxmlpath = p.getProperty("retrievejrxmlpath");
                    generatereportpath = p.getProperty("generatereportpath");

                } 
                catch (IOException ex) 
                {
                    Logger.getLogger(DBConnector.class.getName()).log(Level.SEVERE, null, ex);
                }

    try
    {
        JasperReport jasperReport;
        JasperPrint jasperPrint;
        JasperDesign jasperDesign;
        HashMap parameters = new HashMap();

        jasperReport= JasperCompileManager.compileReport(retrievejrxmlpath+"salesreport.jrxml");

        jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,con);
        
        JasperExportManager.exportReportToPdfFile(jasperPrint,generatereportpath+"salesreport"+dateNow+"-"+timeNow+".pdf");
        
        response.setContentType("application/pdf");
        
        JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
       
        servletOutputStream.flush();
        servletOutputStream.close();
        
    }
    catch (JRException e)
    {
      // display stack trace in the browser
      StringWriter stringWriter = new StringWriter();
      PrintWriter printWriter = new PrintWriter(stringWriter);
      e.printStackTrace(printWriter);
      response.setContentType("text/plain");
      response.getOutputStream().print(stringWriter.toString());
    }
    finally
        {
            
            //close the connection.
            if(con != null)
            {
                try { con.close(); }
                catch (Exception ignored) {}
            }
            
            
        }
  }
}